//
//  ITLoginViewController.m
//  InTubePro
//
//  Created by seven on 2019/12/19.
//  Copyright © 2019 seven. All rights reserved.
//

#import "ITLoginViewController.h"
#import "ITLoginNetworkAPIManager.h"
#import "ITShareManager.h"
#import "HYEdgeInsetsButton.h"
#import "ITURLManager.h"
#import <AuthenticationServices/AuthenticationServices.h>
#import "ITWebViewController.h"
@interface ITLoginViewController () <UITextFieldDelegate, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>{
    
    
    UILabel *_titleLbl;
    UILabel *_sumLbl;
    
    BaseScrollView *_contentView;
    BaseView *_loginView;
    BaseView *_verifyView;
    
    UITextField *_phoneText;
    UIButton *_regionBtn;
    UIButton *_actionBtn;
    
    BOOL _isSendingCode;
    
    UILabel *_fbTipLbl;
    UIButton *_fbBtn;
    
    UITextField *_verifyText;
    UIButton *_smsBtn;
    UIButton *_backBtn;
    NSInteger _smsCountDown;
    
    BOOL _forceGoFb;
    NSMutableDictionary *_sourceInfo;

}
IT_PROPERTY_COPY NSString *mobile;
IT_PROPERTY_COPY NSString *code;
@end

@implementation ITLoginViewController
- (void)dealloc{
    NSLog(@"dealloc");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ColorWithHex(0x16161A);
    //[self.gradientLayer removeFromSuperlayer];
    [self setHideTopView:YES];
    BaseImageView *bgImageView = [BaseImageView initWithFrame:self.view.bounds];
    [self.view addSubview:bgImageView];
    
    NSString *url = [ITGeneralManager.defaultManager configDicWithKey:@"DICT_LOGIN_BG_IMAGE_URL"].value;
    [bgImageView setImageWithUrl:url];
    _sourceInfo = NSMutableDictionary.dictionary;
    
    self.fd_interactivePopDisabled = YES;
    
    self.fd_prefersNavigationBarHidden = YES;
    self.statusBarStyle = [AppUtils getLightStyle];
    
    if (ITURLManager.defaultManager.urlWithState == 0 && !AppUtils.DICT_AU_MODE) {
        [self setupLoginView];
    }else {
        [self setupLoginView2];
    }
    
    UIButton *backBtn = [UIButton initWithFrame:CGRectMake(0, [UIDevice safeAreaTopHeight]+10, 60, 44)];
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    UIImage *img = [[UIImage imageNamed:@"more_close"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [backBtn setImage:img];
    backBtn.tintColor = COLOR_WHITE;
    backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 16, 0, 0);
    [backBtn addTarget:self action:@selector(onBackAction)];
    [self.view addSubview:backBtn];
    
    // Do any additional setup after loading the view.
}

- (void)setupLoginView{
    //*
    BaseView *topView = [UIView initWithFrame:CGRectMake(0, 0, self.view.width, 200+[UIDevice safeAreaTopHeight])];
    
    [self.view addSubview:topView];
    
    BaseImageView *icon = [BaseImageView initWithFrame:CGRectMake((self.view.width-76)/2, UIDevice.safeAreaTopHeight+55, 76, 76)];
    [icon setImageWithName:@"launch_logo"];
    icon.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:icon];
    
    BaseLabel *lbl = [BaseLabel initWithFrame:CGRectMake((self.view.width-300)/2, UIDevice.safeAreaTopHeight+135, 300, 60)];
    lbl.font = defaultFontWithSize(18);
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.textColor = COLOR_WHITE;
    lbl.text = LocalTitle(@"Login to customize your happiness");
    [self.view addSubview:lbl];
//    icon = [BaseImageView initWithFrame:CGRectMake((self.view.width-205)/2, UIDevice.safeAreaTopHeight+135, 205, 60)];
//    [icon setImageWithName:@"log_title"];
//    icon.contentMode = UIViewContentModeScaleAspectFit;
//    [self.view addSubview:icon];
    
    _contentView = [BaseScrollView initWithFrame:CGRectMake(0, topView.bottom, topView.width, self.view.height-topView.bottom-[UIDevice safeAreaBottomHeight])];
    adjustsScrollViewInsets(_contentView);
    _contentView.scrollEnabled = NO;
    [self.view addSubview:_contentView];
    
    CGRect frame = _contentView.bounds;
    _loginView = [BaseView initWithFrame:frame];
    [_contentView addSubview:_loginView];
    
    //BOOL showSignWithApple = [UIDevice systemVersion] >= 13.0;
    BOOL showSignWithApple = YES;
    BOOL showSignWithFb = [ITShareManager isFBInstall];
    //showSignWithApple = showSignWithFb = YES;
    //showSignWithApple = NO;
    if (!showSignWithApple) {
        showSignWithFb = YES;
    }
    if (!showSignWithApple && !showSignWithFb) {
        return;
    }
    
    BaseView *otherView = [BaseView initWithFrame:CGRectMake(0, 100, _loginView.width, 200)];
    [_loginView addSubview:otherView];
    
    UILabel *orLbl = [[UILabel alloc] initWithFrame:CGRectMake((otherView.width-140)/2, 0, 140, 20)];
    //orLbl.backgroundColor = COLOR_WHITE;
    orLbl.textAlignment = NSTextAlignmentCenter;
    orLbl.font = RegularFontWithSize(14);
    orLbl.textColor = COLOR_999999;
    orLbl.text = LocalTitle(@"Continue with");
    [otherView addSubview:orLbl];
    
    [otherView addLineWithFrame:CGRectMake(16, 10, orLbl.left-16, 1) color:ColorWithHexAplha(0xC1C1C1, 0.5)];
    
    [otherView addLineWithFrame:CGRectMake(orLbl.right, 10, otherView.width - orLbl.right-16, 1) color:ColorWithHexAplha(0xC1C1C1, 0.5)];
    
    CGFloat topOffset = 50;
    
    CGFloat leftOffset = 0;
    CGFloat scale = kScreenWidth/375;
    CGFloat space = 72*scale;
    if ((showSignWithApple + showSignWithFb) == 1) {
        leftOffset = (otherView.width-44)/2;
    }else {
        leftOffset = (otherView.width-44*2 - space)/2;
    }
    
    CGRect btnRect = CGRectMake(leftOffset, topOffset, 44, 44);
    
    if (showSignWithApple){
        UIButton *lBtn = [UIButton initWithFrame:btnRect];
        [lBtn setImageWithName:@"apple_icon"];
        [lBtn addTarget:self action:@selector(signWithApple)];
        [otherView addSubview:lBtn];
        
        /*
        BTLabel *lbl = [BTLabel initWithFrame:CGRectMake(0, 0, lBtn.width, 24)];
        lbl.font = defaultFontWithSize(15);
        lbl.textColor = COLOR_333333;
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.text = LocalTitle(@"Apple");
        [lBtn addSubview:lbl];
         //*/
        
        btnRect.origin.x = lBtn.right+space;
    }
    
    if (showSignWithFb) {
        UIButton *lBtn = [UIButton initWithFrame:btnRect];
        [lBtn setImageWithName:@"fb_icon"];
        [lBtn addTarget:self action:@selector(onFbAction)];
        [otherView addSubview:lBtn];
        /*
        BTLabel *lbl = [BTLabel initWithFrame:CGRectMake(0, 0, lBtn.width, 24)];
        lbl.font = defaultFontWithSize(15);
        lbl.textColor = COLOR_333333;
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.text = LocalTitle(@"Facebook");
        [lBtn addSubview:lbl];
         //*/
        
        btnRect.origin.x = lBtn.right+space;
    }

    UIButton *serviceBtn = [UIButton initWithFrame:CGRectMake(_loginView.centerX+30, topOffset+100, 110, 18)];
    [serviceBtn addTarget:self action:@selector(onServiceAction)];
    [otherView addSubview:serviceBtn];
    [serviceBtn addLineWithFrame:CGRectMake(0, serviceBtn.height-0.5, serviceBtn.width, 0.5) color:ColorWithHex(0xFAD015)];
    
    UILabel *tipLbl = [[UILabel alloc] initWithFrame:CGRectMake(serviceBtn.left-200-2, serviceBtn.top, 200, serviceBtn.height)];
    tipLbl.textAlignment = NSTextAlignmentRight;
    [otherView addSubview:tipLbl];
    tipLbl.textColor = COLOR_999999;
    tipLbl.font = RegularFontWithSize(13);
    serviceBtn.titleLabel.font = tipLbl.font;
    [serviceBtn setTitleColor:ColorWithHex(0xFAD015)];
    
    tipLbl.text = LocalTitle(@"By continuing，I accept the");
    [serviceBtn setTitleNormal:LocalTitle(@"service agreement")];

    
}

- (void)setupLoginView2{
    
    BaseView *topView = [UIView initWithFrame:CGRectMake(0, 0, self.view.width, 200+[UIDevice safeAreaTopHeight])];
    [self.view addSubview:topView];

    
    _titleLbl = [UILabel initWithFrame:CGRectMake(16, [UIDevice safeAreaTopHeight]+125, 300, 30)];
    _titleLbl.textColor = COLOR_WHITE;
    _titleLbl.font = SemiboldFontWithSize(22);
    _titleLbl.text = LocalTitle(@"Verify your number");
    [topView addSubview:_titleLbl];
    
    _sumLbl = [UILabel initWithFrame:CGRectMake(_titleLbl.left, _titleLbl.bottom+4, _titleLbl.width, 25)];
    _sumLbl.textColor = COLOR_WHITE;
    _sumLbl.font = RegularFontWithSize(18);
    _sumLbl.text = LocalTitle(@"Enter your phone number below");
    [topView addSubview:_sumLbl];
    
    _contentView = [BaseScrollView initWithFrame:CGRectMake(0, topView.bottom, topView.width, self.view.height-topView.bottom-[UIDevice safeAreaBottomHeight])];
    adjustsScrollViewInsets(_contentView);
    _contentView.scrollEnabled = NO;
    [self.view addSubview:_contentView];
    
    CGRect frame = _contentView.bounds;
    _loginView = [BaseView initWithFrame:frame];
    [_contentView addSubview:_loginView];
    
    frame.origin.x += _loginView.width;
    
    _verifyView = [BaseView initWithFrame:frame];
    [_contentView addSubview:_verifyView];

    _phoneText = [UITextField initWithFrame:CGRectMake(16, 36, kScreenWidth-32, 50)];
    _phoneText.backgroundColor = ColorWithHex(0xFCFCFC);
    [_phoneText setCornerRadius: 4];
    _phoneText.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneText.keyboardType = UIKeyboardTypeNumberPad;
    _phoneText.textColor = COLOR_333333;
    [_phoneText addTarget:self action:@selector(onTextChange:) forControlEvents:(UIControlEventEditingChanged)];
    [_phoneText addTarget:self action:@selector(goSendCode) forControlEvents:(UIControlEventEditingDidEndOnExit)];
    _phoneText.font = RegularFontWithSize(14);
    [_phoneText setBorderColor:ColorWithHex(0xEBEBEB) borderWidth:1];
    _phoneText.placeholder = LocalTitle(@"Enter phone number");
    [_loginView addSubview:_phoneText];
    
    
    [self setupVerifyView];
    
    
    _regionBtn = [UIButton initWithFrame:CGRectMake(0, 0, 48, _phoneText.height)];
    _regionBtn.backgroundColor = ColorWithHex(0xF6F6F6);
    [_regionBtn setTitleColor:COLOR_333333];
    _regionBtn.titleLabel.font = RegularFontWithSize(14);
    [_regionBtn addTarget:self action:@selector(onRegionAction)];
    [_regionBtn setTitleNormal:@"+01"];
    
    BaseView *leftView = [BaseView initWithFrame:CGRectMake(0, 0, _regionBtn.width+15, _regionBtn.height)];
    [leftView addSubview:_regionBtn];
    _phoneText.leftView = leftView;
    _phoneText.leftViewMode = UITextFieldViewModeAlways;
    
    
    _actionBtn = [UIButton initWithFrame:CGRectMake(_phoneText.left, _phoneText.bottom + 14, _phoneText.width, _phoneText.height)];
    _actionBtn.backgroundColor = ColorWithHex(0x9ACBF7);//5B99FD
    _actionBtn.titleLabel.font = SemiboldFontWithSize(16);
    [_actionBtn setTitleColor:COLOR_WHITE];
    [_actionBtn setTitleNormal:LocalTitle(@"Next")];
    [_actionBtn addTarget:self action:@selector(onNextAction)];
    [_loginView addSubview:_actionBtn];
    _actionBtn.userInteractionEnabled = NO;
    
    CAShapeLayer *cornerLayer = [CAShapeLayer layer];
    UIBezierPath *cornerPath = [UIBezierPath bezierPathWithRoundedRect:_actionBtn.bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(20, 20)];
    cornerLayer.path = cornerPath.CGPath;
    cornerLayer.frame = _actionBtn.bounds;
    _actionBtn.layer.mask = cornerLayer;
    
    
    UIButton *serviceBtn = [UIButton initWithFrame:CGRectMake(_loginView.centerX+30, _actionBtn.bottom+16, 104, 18)];
    [serviceBtn addTarget:self action:@selector(onServiceAction)];
    [_loginView addSubview:serviceBtn];
    [serviceBtn addLineWithFrame:CGRectMake(0, serviceBtn.height-0.5, serviceBtn.width, 0.5) color:ColorWithHex(0xffffff)];
    
    UILabel *tipLbl = [[UILabel alloc] initWithFrame:CGRectMake(serviceBtn.left-200-2, serviceBtn.top, 200, serviceBtn.height)];
    tipLbl.textAlignment = NSTextAlignmentRight;
    [_loginView addSubview:tipLbl];
    tipLbl.textColor = COLOR_WHITE;
    tipLbl.font = RegularFontWithSize(13);
    serviceBtn.titleLabel.font = tipLbl.font;
    [serviceBtn setTitleColor:tipLbl.textColor];
    
    tipLbl.text = LocalTitle(@"By continuing，I accept the");
    [serviceBtn setTitleNormal:LocalTitle(@"Terms of Service")];
    
    if (_isBindPhoneNum) {
        return;
    }
    
    BOOL showSignWithApple = [UIDevice systemVersion] >= 13.0;
    BOOL showSignWithFb = [ITShareManager isInstallWithPlatforType:(ITPlatformTypeFB)];
    //showSignWithApple = showSignWithFb = YES;
    //showSignWithApple = NO;
    if (!showSignWithApple && !showSignWithFb) {
        return;
    }
    //*
    BaseView *otherView = [BaseView initWithFrame:CGRectMake(0, _loginView.height-150, _loginView.width, 150)];
    [_loginView addSubview:otherView];
    
    UILabel *orLbl = [[UILabel alloc] initWithFrame:CGRectMake((otherView.width-140)/2, 0, 140, 20)];
    //orLbl.backgroundColor = COLOR_WHITE;
    orLbl.textAlignment = NSTextAlignmentCenter;
    orLbl.font = RegularFontWithSize(14);
    orLbl.textColor = COLOR_WHITE;
    orLbl.text = LocalTitle(@"Or sign in with:");
    [otherView addSubview:orLbl];
    
    [otherView addLineWithFrame:CGRectMake(16, 10, orLbl.left-16, 1) color:ColorWithHexAplha(0xC1C1C1, 0.5)];
    
    [otherView addLineWithFrame:CGRectMake(orLbl.right, 10, otherView.width - orLbl.right-16, 1) color:ColorWithHexAplha(0xC1C1C1, 0.5)];
    
    CGFloat topOffset = 50;
    
    CGFloat leftOffset = 0;
    CGFloat scale = kScreenWidth/375;
    CGFloat space = 72*scale;
    if ((showSignWithApple + showSignWithFb) == 1) {
        leftOffset = (otherView.width-44)/2;
    }else {
        leftOffset = (otherView.width-44*2 - space)/2;
    }
    
    CGRect btnRect = CGRectMake(leftOffset, topOffset, 44, 44);
    
    if (showSignWithApple){
        UIButton *lBtn = [UIButton initWithFrame:btnRect];
        [lBtn setImageWithName:@"apple_icon"];
        [lBtn addTarget:self action:@selector(signWithApple)];
        [otherView addSubview:lBtn];
        
        btnRect.origin.x = lBtn.right+space;
    }
    
    if (showSignWithFb) {
        UIButton *lBtn = [UIButton initWithFrame:btnRect];
        [lBtn setImageWithName:@"fb_icon"];
        [lBtn addTarget:self action:@selector(onFbAction)];
        [otherView addSubview:lBtn];
        btnRect.origin.x = lBtn.right+space;
    }
    //*/
}

- (void)setupVerifyView{
    
    _verifyText = [UITextField initWithFrame:_phoneText.frame];
    [_verifyText setCornerRadius: 4];
    _verifyText.textColor = COLOR_WHITE;
    _verifyText.textAlignment = NSTextAlignmentCenter;
    _verifyText.font = RegularFontWithSize(14);
    [_verifyText setBorderColor:ColorWithHex(0xEBEBEB) borderWidth:1];
    //_verifyText.placeholder = LocalTitle(@"Enter SMS PIN");
    _verifyText.attributedPlaceholder = [NSAttributedString generateAttriuteStringWithStr:LocalTitle(@"Enter SMS PIN") WithColor:COLOR_WHITE WithFont:_verifyText.font];
    [_verifyText addTarget:self action:@selector(onTextChange:) forControlEvents:(UIControlEventEditingChanged)];
    _verifyText.keyboardType = UIKeyboardTypeNumberPad;
    [_verifyView addSubview:_verifyText];
    
    UILabel *tipLbl = [UILabel initWithFrame:CGRectMake(20, _verifyText.bottom+20, kScreenWidth-40, 18)];
    tipLbl.textColor = COLOR_WHITE;
    tipLbl.font = RegularFontWithSize(13);
    tipLbl.text = LocalTitle(@"Check your SMS for PIN");
    [_verifyView addSubview:tipLbl];
    
    
    _smsBtn = [UIButton initWithFrame:CGRectMake(_verifyText.right-134, tipLbl.bottom + 24, 134, 40)];
    _smsBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_smsBtn setTitleColor:COLOR_WHITE];
    _smsBtn.titleLabel.font = MediumFontWithSize(13);
    [_smsBtn addTarget:self action:@selector(onSmsAction)];
    [_smsBtn setTitleNormal:LocalTitle(@"Resend SMS")];
    [_verifyView addSubview:_smsBtn];
    
    _backBtn = [UIButton initWithFrame:CGRectMake(0, _smsBtn.centerY-10, 138, 20)];
    _backBtn.titleLabel.font = MediumFontWithSize(13);
    [_backBtn setTitleColor:ColorWithHex(0x5B99FD)];
    [_backBtn setTitleNormal:LocalTitle(@"Wrong Number？")];
    [_backBtn addTarget:self action:@selector(onBackAction)];
    [_verifyView addSubview:_backBtn];
    WEAKSELF
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weak_self.view endEditing:YES];
    }];
    [self.view addGestureRecognizer:tap];
}

- (void)onNextAction{
    [self goSendCode];
}


- (void)goVerify{
    
    [_smsBtn setTitleColor:COLOR_WHITE];
    _smsCountDown = 60;
    [self smsCountDownLoop];
    
    [_contentView setContentOffset:CGPointMake(_contentView.width, 0) animated:YES];
    
    _titleLbl.text = [AppUtils titleForKey:@"Enter PIN"];
    _sumLbl.text = [AppUtils titleForKey:@"Enter the 4-Digit PIN we sent"];
    
    [_verifyText becomeFirstResponder];
    
}

- (void)backToLogin{
    [_contentView setContentOffset:CGPointMake(0, 0) animated:YES];
    
    _titleLbl.text = [AppUtils titleForKey:@"Verify your number"];
    _sumLbl.text = [AppUtils titleForKey:@"Enter your phone number below"];
}

- (void)onRegionAction{
    
}

- (void)onServiceAction{
    
    ITWebViewController *web = [[ITWebViewController alloc] init];
    web.URLString = [AppUtils configDicWithKey:DICT_WEB_PRIVACY];
    web.title = @"Terms of Service";
    [self.navigationController pushViewController:web animated:YES];
    //[AppUtils pushWebView:@"" title:@"Terms of Service"];
}

- (void)onSmsAction{
    if (_smsCountDown < 1) {
        [self goSendCode];
    }
}

- (void)onBackAction{
    if (_contentView.contentOffset.x == 0) {

        [ITUserManager.shareManager cancelLogin];
        [self dismissLoginView];
        
        return;
    }
    [self backToLogin];
    _verifyText.text = nil;
    //_phoneText.text = nil;
    [self onTextChange:_phoneText];
}

- (void)onTextChange:(UITextField *)tf{
    NSString *tmp = tf.text.stringByTrim;
    if (tf == _verifyText) {
        if (tmp.length == 4) {
            _code = tmp;
            [self loginWithMobile];
        }
    }else {
        if (tmp.length > 3) {
            _actionBtn.userInteractionEnabled = YES;
            _actionBtn.backgroundColor = ColorWithHex(0x5B99FD);//5B99FD
        }else {
            _actionBtn.userInteractionEnabled = NO;
            _actionBtn.backgroundColor = ColorWithHex(0x9ACBF7);//5B99FD
        }
    }
}


- (void)dismissLoginView{
    
    if (_lastLandscaped) {
        ITOrientationManager.sharedInsatnce.supportLandspcape = YES;
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma mark - 登录

- (void)loginWithMobile{
    [self goLoginWithParams:@{@"source": @"app",
                              @"mobile": _mobile,
                              @"verifycode": _code,
    }];
}

- (void)goLoginWithParams:(NSDictionary *)params{
    [self.view showLoading];
    WEAKSELF
    [ITLoginNetworkAPIManager.shareManager loginWithParams:params callback:^(ITNetWorkModel *ret) {
        [self.view hideLoading];
        STRONGSELF
        if (self == nil) {
            return ;
        }
        if (ret.success) {
            ITUserInfo *user = [ITUserInfo modelWithDictionary:ret.data];
            user.mobile = weak_self.mobile;
            [ITUserManager.shareManager loginSuccess:user];
            [weak_self dismissLoginView];
        }else {
            self->_sourceInfo[@"error"] = ret.errorMsg;
        }

        self->_sourceInfo[@"result"] = ret.success ? @"1" : @"2";
        [AppUtils trackMTAEventNo:@"wk_00102" parameters:self->_sourceInfo];
    }];
}

#pragma mark - 验证码
- (void)goSendCode{
    if (_isSendingCode) {
        return;
    }
    //获取验证码
    _mobile = [_phoneText.text stringByTrim];
    //_mobile = @"7123456789";
    if (_mobile.length < 4) {
        [AppUtils showInfo:LocalTitle(@"Invalid mobile")];
        return;
    }
    _isSendingCode = YES;
    [self.view showLoading];
    WEAKSELF
    [[ITLoginNetworkAPIManager shareManager] sendValidateCodeWithMobile:_mobile rand:@"login" callback:^(ITNetWorkModel *ret) {
        self->_isSendingCode = NO;
        STRONGSELF
        if (self == nil) {
            return ;
        }
        if (ret.success) {
            [weak_self goVerify];
        }else{
            [AppUtils showInfo:ret.message ?: [AppUtils titleForKey:@"network is shutdown"] position:CSToastPositionCenter];
        }
        [self.view hideLoading];
    }];
}

- (void)smsCountDownLoop{
    _smsCountDown--;
    if (_smsCountDown > 0) {
        NSString *time = [@(_smsCountDown) convertTimeString];
        [_smsBtn setTitleNormal:[AppUtils titleForKey:@"Resend SMS %@", time]];
        MainDelay(1, ^{
            [self smsCountDownLoop];
        });
    }else {
        [_smsBtn setTitleNormal:LocalTitle(@"Resend SMS")];
        [_smsBtn setTitleColor:ColorWithHex(0x5B99FD)];
    }
}


#pragma mark - sign with apple
/*
- (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller{
    return UIApplication.sharedApplication.keyWindow;
}

- (void)signWithApple{
    
    _sourceInfo[@"type"] = @"apple";
    [AppUtils trackMTAEventNo:@"wk_00103" parameters:_sourceInfo];
    
    if ([UIDevice systemVersion] < 13.0) {
        [self.view showInfo:LocalTitle(@"Cannot be used below IOS 13")];
        return;
    }
    
    // 创建请求
    ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
    ASAuthorizationAppleIDRequest *request = appleIDProvider.createRequest;
    [request setRequestedScopes:@[ASAuthorizationScopeFullName,ASAuthorizationScopeEmail]];
        
    // A mechanism for generating requests to perform keychain credential sharing.
           // 为了执行钥匙串凭证分享生成请求的一种机制
    //ASAuthorizationPasswordRequest *passwordRequest = [[ASAuthorizationPasswordProvider new] createRequest];
    // 发起请求
    ASAuthorizationController *controller = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];//, passwordRequest]];
    controller.delegate = self;
    controller.presentationContextProvider = self;
    [controller performRequests];

    
}


- (void)authorizationController:(ASAuthorizationController *)controller
   didCompleteWithAuthorization:(ASAuthorization *)authorization {
        
    //Sign with Apple 成功
        
    if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
        //此时为使用Sign With Apple 方式登录
        ASAuthorizationAppleIDCredential *credential = authorization.credential;
        NSString *userID = credential.user;
        NSString *token = [credential.identityToken utf8String];
//        NSData *code = credential.authorizationCode;
//        NSString *c = [code utf8String];
        NSPersonNameComponents *fullname = credential.fullName;
        NSString *name = fullname.givenName;
        if (fullname.familyName.length>0) {
            name = [name stringByAppendingFormat:@"%@",fullname.familyName];
        }
        NSMutableDictionary *parms = NSMutableDictionary.dictionary;
        parms[@"source"] = @"apple";
        parms[@"accessToken"] = token;
        parms[@"nickname"] = name;
        parms[@"openid"] = userID;
        [self goLoginWithParams:parms];
        //继续进行客户端后台登录验证
    }
}
//*/
- (UIInterfaceOrientationMask)supportedInterfaceOrientations1{
    return UIInterfaceOrientationMaskPortrait;
    //return UIInterfaceOrientationMaskAllButUpsideDown;
}

@end
